#!/bin/bash
VM_OPS_SCRIPTS_DIR="/root/xen/vm_ops_scripts"
source $VM_OPS_SCRIPTS_DIR/utils/env.bash
source $VM_OPS_SCRIPTS_DIR/start_vm/scene.bash
function clean_all_ops(){
local VM_ID=$1
LOG_MSG "destroy vm if need"
xm des vm-$VM_ID >/dev/null 2>&1
sleep 1
LOG_MSG "delete the work folder if need"
rm -rf $VMS_WORK_DIR/$VM_DIR >/dev/null 2>&1

}

LOG_MSG "destroy vm ops begin..."

if [ $# != 1 ]; then
    LOG_MSG  "$* parameter error";
    exit 255
fi  

VM_ID=$1
VM_NAME=vm-$VM_ID
VM_DIR=VM-$VM_ID

if xm domid $VM_NAME
then
   LOG_MSG "$VM_NAME has exist"
   clean_all_ops $VM_ID
   if [ $? -ne 0 ];then
      LOG_MSG "destroy vm:$VM_NAME failed"
      exit 255
   fi
else
   LOG_MSG "$VM_NAME not exist"
   #exit 1
fi

